/*
 * @(#)ForwardResult.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.result;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nhncorp.lucy.web.helper.ServletHelper;

import com.nhncorp.usf.core.config.runtime.ResultPageInfo;
import com.nhncorp.usf.core.util.StringUtil;

/**
 * 다른 경로로의 Forward 수행을 위한 {@link Result}.
 *
 * @author Web Platform Development Team
 */
@SuppressWarnings("serial")
public class ForwardResult extends AbstractResult {
    /**
     * 전달된 {@link com.nhncorp.lucy.common.util.DataMap} 정보를 {@link ResultPageInfo} 에 적용하여 결과 페이지 내보냄.
     *
     * @param resultPage the page information
     * @param dataMap    the usf result data
     * @throws Exception the Exception
     */
    @SuppressWarnings("unchecked")
	public void execute(ResultPageInfo resultPage, Map<String, Object> dataMap) throws Exception {
        HttpServletRequest request = ServletHelper.getRequest();
        HttpServletResponse response = ServletHelper.getResponse();

        String src;
        src = StringUtil.getNewSrc(resultPage.getSrc(), dataMap);

        String encodedPath = response.encodeURL(src);

        // dataMap 정보를 request의 attribute 에 저장( parameterMap에 없는것만)
        Map parameterMap = request.getParameterMap();

        for (Map.Entry<String, Object> entry : dataMap.entrySet()) {

            if (!parameterMap.containsKey(entry.getKey())) {
                request.setAttribute(entry.getKey(), entry.getValue());
            }

        }

        request.getRequestDispatcher(encodedPath).forward(request, response);
    }
}
